define([
    'base',
    'plugins/ztree/ztree'
], function (base) {
    //默认加载基础库
    base();

    /**
     * form表单转换成json
     */
    function getFormJson(form) {
        var data = {};
        var a = $(form).serializeArray();
        $.each(a, function () {
            if (data[this.name] !== undefined) {
                if (!data[this.name].push) {
                    data[this.name] = [data[this.name]];
                }
                data[this.name].push(this.value || '');
            } else {
                data[this.name] = this.value || '';
            }
        });
        return data;
    }



    return {
        update: function () {
            //添加流程步骤
            $("#addStep").on('click', function () {
                var length = $("form").length + 1;
                $("#step-box").append("<div class=\"panel panel-default\"><div class=\"panel-heading\"><b class=\"text step-title\">流程步骤"+length+"：</b><div class=\"pull-right\"><button onclick=\"$(this).parents('.panel').remove()\" class=\"btn btn-danger\">&nbsp;删除&nbsp;</button></div></div><div class=\"panel-body\"><form class=\"form-horizontal\" name=\"flowTemplateForm\" id=\"flowTemplate_"+length+"\"><div class=\"form-group\"><div class=\"col-sm-3\"><label class=\"control-label\">步骤名称：</label><input name=\"stepName\" class=\"form-control\" type=\"text\" required aria-required=\"true\" maxlength='30' placeholder=\"请输入步骤名称\"></div><div class=\"col-sm-3\"><label class=\"control-label\">上个部门小组编号：</label><input name=\"lastOrgId\" class=\"form-control\" type=\"number\" placeholder=\"请输入上个部门小组编号\" required='required'></div><div class=\"col-sm-3\"><label class=\"control-label\">当前部门小组编号：</label><input name=\"orgId\" class=\"form-control\" type=\"number\" required aria-required=\"true\" placeholder=\"请输入当前部门小组编号\"></div><div class=\"col-sm-3\"><label class=\"control-label\">下个部门小组编号：</label><input name=\"nextOrgId\" class=\"form-control\" type=\"text\" placeholder=\"请输入下个部门小组编号\" required='required'></div></div><div class=\"form-group\"><div class=\"col-sm-3\"><label class=\"control-label\">节点类型：</label><select name=\"nodeType\" class=\"form-control\"><option value=\"0\" selected>普通节点</option><option value=\"1\">开始节点</option><option value=\"2\">结束节点</option></select></div><div class=\"col-sm-9\"><label class=\"control-label\">附加参数：</label><input name=\"attrs\" class=\"form-control\" type=\"text\" required aria-required=\"true\" placeholder=\"请输入附加参数\"></div></div></form></div></div>");
            });
            //提交流程
            $("#doSubmit").on('click', function () {
                    var dataFormStatus = false;
                    var form = $("form[name='flowTemplateForm']");
                    // $.each(form, function () {
                    //     dataFormStatus = $(this).valid();
                    // });
                     dataFormStatus = true;
                    if (dataFormStatus) {
                        var length = form.length;
                        //定义要传递的数组
                        var flowTemplateList = [];
                        for (var i = 1; i <= length; i++) {
                            var formJson = getFormJson($("#flowTemplate_" + i));
                            flowTemplateList.push(formJson);
                        }
                        $.ajax({
                            url: "/flowTemplate/save/action2",
                            type: 'POST',
                            // data: JSON.stringify(flowTemplateList),
                            data: {
                                'flowTemplateJson':JSON.stringify(flowTemplateList),
                                'itemType':"GONGSHAGN_APPLY"
                            },
                            dataType: "JSON",
                            // contentType: 'application/json;charset=utf-8',
                            beforeSend: function () {
                                //触发ajax请求开始时执行
                                $("#doSubmit").text('提交中...');
                                $("#doSubmit").attr('disabled', 'disabled');
                            },
                            success: function (data) {
                                if (data.success) {
                                    window.location.href = "/flowTemplate/view/index";
                                } else {
                                    msgTool.error(data.msg);
                                }
                            },
                            error: function (error) {
                                msgTool.error("操作失败，请刷新当前页面后重试！");
                            },
                            complete: function () {
                                $("#doSubmit").text("下一步");
                                $("#doSubmit").removeAttr('disabled');
                            }
                        });
                    }
                }
            );
        }
    }
});






